package com.duowei.diandan.print2;


import android.os.Handler;
import android.text.TextUtils;
import android.widget.Toast;

import com.android.print.sdk.PrinterConstants;
import com.android.print.sdk.PrinterInstance;
import com.android.print.sdk.Table;
import com.duowei.diandan.app.App;
import com.duowei.diandan.app.InitManager;
import com.duowei.diandan.app.OrderList;
import com.duowei.diandan.bean.Payment;
import com.duowei.diandan.bean.QrCodePay;
import com.duowei.diandan.constant.PayType;
import com.duowei.diandan.db.AppDatabase;
import com.duowei.diandan.db.entry.Jgsz;
import com.duowei.diandan.db.entry.Wmlsb;
import com.duowei.diandan.db.entry.Wmlsbjb;

import java.util.List;
import java.util.Locale;

public class InsidePrint {
    private Handler mHandler;

    public InsidePrint(Handler handler) {
        mHandler = handler;
    }

    public void print(Wmlsbjb wmlsbjb, List<Wmlsb> wmlsbList, boolean isCommit) {
        PrinterInstance printer = InitManager.getInstance().getOperation().getPrinter();

        if (printer != null) {
            printer.init();
            printer.setPrinter(PrinterConstants.Command.LINE_HEIGHT, 80);
//            printer.setPrinter(PrinterConstants.Command.CHARACTER_RIGHT_MARGIN, 2);

            Jgsz jgsz = AppDatabase.getDatabase().jgszDao().findOne();
            if (jgsz != null && !TextUtils.isEmpty(jgsz.getBmmc())) {
                printer.setLeftMargin(0, 0);
                printer.setPrinter(PrinterConstants.Command.ALIGN, 1);
                printer.setPrinter(PrinterConstants.Command.FONT_SIZE, 2);
                printer.printText(jgsz.getBmmc() + "\n\n");
                printer.setPrinter(PrinterConstants.Command.FONT_SIZE, 0);
                printer.setPrinter(PrinterConstants.Command.ALIGN, 0);
            }


            printer.setLeftMargin(65, 0);
            printer.setPrinter(PrinterConstants.Command.FONT_SIZE, 1);
            printer.printText("桌号:" + wmlsbjb.getZh() + "\n");
            printer.setPrinter(PrinterConstants.Command.FONT_SIZE, 0);
            printer.printText("账单号:" + wmlsbjb.getWmdbh() + "\n");
            printer.printText("日期:" + wmlsbjb.getJysj().toString("yyyy-MM-dd HH:mm:ss") + "\n");

            printer.printText("-----------------------------------\n");
            Table table = new Table("单品名称,数量,金额", ",", new int[]{22, 6, 8});
            for (Wmlsb w : wmlsbList) {
                String xmmc = w.getXmmc();
                if (xmmc.startsWith(" ")) {
                    table.addRow("·" + w.getXmmc().replace(" ", "") + "," + w.getSl() + ",￥" + w.getDj());
                } else {
                    table.addRow(w.getXmmc() + "," + w.getSl() + ",￥" + w.getDj());
                }

                if (!TextUtils.isEmpty(w.getPz())) {
                    table.addRow(w.getPz());
                }
            }
            printer.printTable(table);
            printer.printText("-----------------------------------\n");

            if (isCommit) {
                printer.printText(String.format(Locale.CHINA, "消费合计: ￥%.2f\n", wmlsbjb.getYs()));
                printer.printText("-----------------------------------\n");
            } else {
                printer.printText(String.format(Locale.CHINA, "消费总额: ￥%.2f\n", wmlsbjb.getYsje()));
                printer.printText(String.format(Locale.CHINA, "折扣金额: ￥%.2f\n", wmlsbjb.getZk()));
                printer.printText(String.format(Locale.CHINA, "应收金额: ￥%.2f\n", wmlsbjb.getYs()));
                printer.printText("-----------------------------------\n");

                List<Payment> paymentList = OrderList.getInstance().getPaymentList();
                if (paymentList != null && paymentList.size() > 0) {
                    for (Payment p : paymentList) {
                        if (p.payType == PayType.STORED_VALUE_TYPE) {
                            printer.printText(String.format(Locale.CHINA, "%s  ￥%.2f\n", p.title, p.payMoney));
                        } else if (p.payType == PayType.INTEGRAL_TYPE) {
                            printer.printText(p.title + "  " + p.jfValue + "\n");
                        } else if (p.payType == PayType.COUPON_TYPE) {
                            printer.printText(p.title + "  " + p.ticketNum + "张\n");
                        }
                    }
                }

                QrCodePay qrCodePay = OrderList.getInstance().getQrCodePay();
                if (qrCodePay != null) {
                    printer.printText(String.format(Locale.CHINA, "%s  ￥%.2f\n", qrCodePay.getTitle(), qrCodePay.getPrice()));
                }

                printer.printText("-----------------------------------\n");

                printer.setPrinter(PrinterConstants.Command.ALIGN, 1);
                printer.setLeftMargin(0, 0);
                printer.printText("谢谢惠顾\n");
            }


            printer.setPrinter(PrinterConstants.Command.PRINT_AND_WAKE_PAPER_BY_LINE, 1);
            printer.cutPaper();
        } else {
            mHandler.post(() -> {
                Toast.makeText(App.getApp(), "未发现内置打印机", Toast.LENGTH_SHORT).show();
            });
        }
    }
}
